/**
 * 公共配置js
*/


//公共配置部分
var commonInfo={
    ads:[
        {url:"uploads/ads/summer18.jpg",link:"http://172.168.0.248:3000/index.html",title:"机器人上线了"}
    ],
    //背景音乐
    bgsound:'./uploads/song/bg1805.mp3',
    //即将上线宠物广告
    comingpet:[
        {name:"机器人",imgurl:"uploads/pet/coming/jiqiren.png"},
        {name:"铁蛋",imgurl:"uploads/pet/coming/lu.png"},
    ],
    //加载页面背景广告
    loadingBg:"uploads/loading/loading171228.jpg"
};

//广告
// var ads=[
//     {url:"uploads/ad/ad2.jpg",link:"http://www.baidu.com",title:"夜华上线"},
//{url:"uploads/ad/draw06_2.jpg",link:"http://shop.mobby.cn/html/active/draw06/ranking.html?from=show",title:"我画我豆排行榜"},
// ];

var isShow=false;//是否显示访问广告,false:不显示;true:是否显示广告
var adindex=0;
var ads = commonInfo.ads;
var adlen=ads.length;
function setindexad(i){
    var linkstr='';
    if(ads[i].link){
        linkstr=ads[i].link;
    }
    var adstr='<div class="adPage" id="adPage"><div id="adImgBox"><a href="javascript:;" id="closeAd"></a><a href="javascript:;" id="imgBoxLink" data-url="'+linkstr+'" data-title="'+ads[i].title+'" style="background-image:url('+ads[i].url+')"></a></div></div>';
    document.body.insertAdjacentHTML("beforeend",adstr);
}

if(isShow){
    if(adlen>0){
        setindexad(adindex);
    }
}

document.body.onclick = function(ev){
    var ev = ev || window.event;
    var target = ev.target || ev.srcElement;

    //关闭广告
    if(target.id== 'closeAd'){
        closeAdbox();
    }
    //关闭加载页面
    if(target.id== 'loadIframeClose'){
        document.getElementById("loadIframe").setAttribute("class","loadIframeOut");
        window.setTimeout(function(){
            document.getElementById("loadIframe").remove();
        },500);
    }

    //点击图片
    if(target.id=='imgBoxLink'){
        var s_title=target.getAttribute("data-title");
        var s_url=target.getAttribute("data-url");
        if(s_url!=""){
        	s_url+="&uid="+query_uid+"&area="+query_area+"&rnd="+Math.floor(Math.random()*10000);
            showIframe(s_title,s_url);
            window.setTimeout(function(){
                closeAdbox();
            },500);
        }
    }
};

//关闭广告，并判断是否有下一个
function closeAdbox(){
    document.getElementById("adPage").remove();
    adindex++;
    if(adindex<adlen){
        setindexad(adindex);
    }
}

//设置并弹出加载页面
function showIframe(title,url){
    var w=document.body.offsetWidth;
    var h=document.body.offsetHeight;
    ifh=h-60;
    var iframeStr='<div id="loadIframe" class="loadIframeIn"><div class="loadIframe-top"><div id="loadIframeClose">返回</div><h2>'+title+'<h2></div>'+
        '<div class="loadIframe-box" id="loadIframeBox"><iframe id="IframeUrl" src="'+url+'" allowtransparency="true" style="width:'+w+'px; height:'+ifh+'px;" scrolling="yes"></iframe></div></div>';
    document.body.insertAdjacentHTML("beforeend",iframeStr);
    reinitIframe();
}

var oldh=0;
 
//重置Iframe高度
var reinitIframe =  function(){
    var iframe = document.getElementById("IframeUrl");
    // if (iframe.attachEvent){ 
    //     iframe.attachEvent("onload", function(){ 
    //        console.log("1Local iframe is now loaded."); 
    //        setH()
    //     }); 
    // } else { 
    //     iframe.onload = function(){ 
    //         console.log("2Local iframe is now loaded."); 
    //         setH();
    //     }; 
    // } 
   
    //function setH(){
        var hash = window.location.hash.slice(1);  
        if (hash && /height=/.test(hash)) {
            var h = hash.replace("height=", "");
            iframe.height=h; 
            iframe.style.height=h+"px";
            if(h!=oldh){
                oldh=h;
                document.getElementById("loadIframeBox").scrollTop=0;
            }
        }else{
            iframe.height=iframe.contentWindow.document.body.scrollHeight;  
            iframe.style.height=iframe.contentWindow.document.body.scrollHeight+"px";  
        }
    //}

    window.setTimeout(function(){
        reinitIframe();
    }, 400);
}

